#include "uart.h"
#include "main.h"
#include "can_motor_user.h"
#include "chassis_task.h"
#include "usart.h"


static void uart3_callback();
static void uart6_callback();

/*	外部变量&头文件链接区 START	*/
extern int start;
extern uint8_t run_flag;
extern uint8_t inverse_flag;
extern uint8_t stop_flag;
extern uint8_t op_pump_flag;
extern uint8_t cl_pump_flag;
/*	外部变量&头文件链接区 END	*/

//用于串口接收
uint8_t uart_rx;		//串口6
uint8_t uart3_rx;		//串口3

uint8_t Rx_buf[24];			//存接收数据(uart6)
uint8_t Rx3_buf[10];		//存接收数据(uart3)

uint8_t  R_time = 0;	//记录第几个数据	(uart6)
uint8_t  Count = 0;		//记录第几个数据	(uart3)

uint8_t  flag=0;		//接收标志(uart6)
uint8_t  flag_3=0;		//接收标志(uart3)



/*	串口6接收数据 START	*/
uint8_t close_flag = 0;
uint8_t start_flag = 0;

extern uint8_t angle_flag;

//手柄键位
uint8_t xbox_A=0;
uint8_t xbox_Y=0;
uint8_t xbox_B=0;
uint8_t xbox_X=0;
uint8_t xbox_Download=0;
uint8_t xbox_select=0;
uint8_t xbox_menu=0;
uint8_t xbox_logo=0;
uint8_t xbox_LB=0;
uint8_t xbox_LS=0;
uint8_t xbox_RB=0;
uint8_t xbox_RS=0;
uint8_t xbox_Left=0;
uint8_t xbox_Right=0;
uint8_t xbox_Up=0;
uint8_t xbox_Down=0;
uint8_t xbox_LT=0;
uint8_t xbox_RT=0;

//断联检测
uint8_t connect_flag;//接收包尾校验位
uint8_t bluetooth_flag;//蓝牙状态标志位
#define BLUETOOTH_CONNECTED 0xAA
#define BLUETOOTH_DISCONNECTED  0xBB


//串口断联检测标志位
uint8_t uart6_flag=0;
/*	串口6接收数据 END	*/


/*	串口3接收数据 START	*/
uint8_t x_signal;
uint8_t x_h ;
uint8_t x_l;
uint8_t y_signal;
uint8_t y_h ;
uint8_t y_l;
uint8_t angle_signal;
uint8_t angle_h;
uint8_t angle_l;
/*	串口3接收数据 END	*/


/**
 * @brief 串口3的中断回调
 */
static void uart3_callback()
{
	if(uart3_rx == 0x55 && flag_3 ==0)  flag_3 = 1;

	if (flag_3 == 1)
	{
		Rx3_buf[Count] = uart3_rx;
		Count++;

		if(Count == 10 )
		{
			x_signal=Rx3_buf[1];
			x_h =Rx3_buf[2];
			x_l=Rx3_buf[3];
			y_signal=Rx3_buf[4];
			y_h=Rx3_buf[5] ;
			y_l=Rx3_buf[6];
			angle_signal=Rx3_buf[7];
			angle_h=Rx3_buf[8];
			angle_l=Rx3_buf[9];
			Count = 0;
			flag_3 = 0;
		}
	}
	HAL_UART_Receive_IT(&huart3, (uint8_t *)&uart3_rx, 1);
}

/**
 * @brief 串口6的中断回调
 */
static void uart6_callback()
{
    if(uart_rx == 0x55 && flag ==0)  flag = 1;
    if (flag == 1)
    {
        Rx_buf[R_time] = uart_rx;
        R_time++;

        //接收够24帧数据后才处理
        if(R_time == 24 )
        {

            //断联检测
            connect_flag=Rx_buf[23];
            if(connect_flag == BLUETOOTH_CONNECTED)
            {
                bluetooth_flag = 1;
            }
            else
            {
                bluetooth_flag = 0;
            }

            //处理底盘移动

            x = ((float)Rx_buf[17] / 128) * 10 - 10;
            y = ((float)Rx_buf[18] / 128) * 10 - 10;
            z = ((float)Rx_buf[19] / 128) * 10 - 10;
            //遥杆死区控制
            if ( x< 5 && x > -5 ) x = 0;
            if ( y< 5 && y > -5 ) y = 0;
            if ( z< 5 && z > -5 ) z = 0;

            //更新按键

            xbox_A 			= Rx_buf[1];
            xbox_Y 			= Rx_buf[2];
            xbox_B 			= Rx_buf[3];
            xbox_X 			= Rx_buf[4];
            xbox_Download 	= Rx_buf[5];
            xbox_select 	= Rx_buf[6];
            xbox_menu 		= Rx_buf[7];
            xbox_logo 		= Rx_buf[8];
            xbox_LB 		= Rx_buf[9];
            xbox_LS 		= Rx_buf[10];
            xbox_RB 		= Rx_buf[11];
            xbox_RS 		= Rx_buf[12];
            xbox_Left 		= Rx_buf[13];
            xbox_Right 		= Rx_buf[14];
            xbox_Up 		= Rx_buf[15];
            xbox_Down 		= Rx_buf[16];

            xbox_LT=Rx_buf[21];
            xbox_RT=Rx_buf[22];





//            //摩擦轮按键
//            if(xbox_Y == 1)
//            {
//                run_flag = 1;//正传
//            }
//            if(xbox_B == 1)
//            {
//                inverse_flag = 1;//反转
//            }
//            if(xbox_X == 1)
//            {
//                stop_flag= 1;//停止
//            }


            //气泵按键
            if(xbox_Download == 1)
            {
                op_pump_flag = 1;//打开气泵
            }
            if(xbox_select == 1)
            {
                cl_pump_flag = 1;//关闭气泵
            }

            //发射
            //发射按键
            extern uint8_t shoot_pull_flag;						//拉弹簧标志位		(点动)		上
            extern uint8_t shoot_refresh_poi_flag;		//让电机归位标志位	    (点动)		左
            extern uint8_t shoot_servo_lock_flag;			//舵机勾着 			(点动)		下
            extern uint8_t shoot_servo_unlock_flag;		//舵机释放			(点动)		右
            extern uint8_t m2006_up_flag;							//2006让连杆向上		(点动)		xbox_RB
            extern uint8_t m2006_down_flag;						//2006让连杆向下		(点动)		xbox_LB
            extern uint8_t auto_run_flag;             //自动化发射          (点动)      三个横杠键
            if(xbox_RB == 1)
            {
                m2006_up_flag = 1;
            }
            if(xbox_LB == 1)
            {
                m2006_down_flag = 1;
            }

            if(xbox_Left == 1)
            {
                shoot_refresh_poi_flag 	= 1;
            }
            if(xbox_Up == 1)
            {
                shoot_pull_flag = 1;
            }
            if(xbox_Right == 1)
            {
                shoot_servo_unlock_flag = 1;
            }
            if(xbox_Down == 1)
            {
                shoot_servo_lock_flag = 1;
            }


            //清除接收数据的位数与标志
            R_time = 0;
            flag = 0;

        }


    }

    HAL_UART_Receive_IT(&huart6, (uint8_t *)&uart_rx, 1);
}

/**
 * @brief HAL库接收中断回调
 */
extern "C" void HAL_UART_RxCpltCallback  (UART_HandleTypeDef *huart)
{
	if(huart == &huart6)
	{
		uart6_flag=1;
		uart6_callback();
    }


	if(huart == &huart3)
	{
		uart3_callback();
	}
}





